package cn.sstir.hackathon.wrapper;

import cn.sgst.tool.common.page.Page;

import java.util.List;
import java.util.Map;

/**
 *
 *
 *
 * @author: fli
 * @email: fli@sstir.cn
 * @date: 2019/12/27 13:15
 */
public abstract class BaseControllerWrapper {
    private Page<Map<String, Object>> page = null;

    private cn.sgst.tool.common.page.Page<Map<String, Object>> pageResult = null;

    private Map<String, Object> single = null;

    private List<Map<String, Object>> multi = null;

    public BaseControllerWrapper(Map<String, Object> single) {
        this.single = single;
    }

    public BaseControllerWrapper(List<Map<String, Object>> multi) {
        this.multi = multi;
    }


    public BaseControllerWrapper(cn.sgst.tool.common.page.Page<Map<String, Object>> pageResult) {
        if (pageResult != null && pageResult.getRecords() != null) {
            this.pageResult = pageResult;
            this.multi = pageResult.getRecords();
        }
    }

    @SuppressWarnings("unchecked")
    public <T> T wrap() {

        /**
         * 包装结果
         */
        if (single != null) {
            wrapTheMap(single);
        }
        if (multi != null) {
            for (Map<String, Object> map : multi) {
                wrapTheMap(map);
            }
        }

        /**
         * 根据请求的参数响应
         */
        if (page != null) {
            return (T) page;
        }
        if (pageResult != null) {
            return (T) pageResult;
        }
        if (single != null) {
            return (T) single;
        }
        if (multi != null) {
            return (T) multi;
        }

        return null;
    }

    protected abstract void wrapTheMap(Map<String, Object> map);
}
